/*
 * @Descripttion: 
 * @version: 
 * @Author: 尹伊
 * @Date: 2020-12-22 10:05:43
 * @LastEditors: 尹伊
 * @LastEditTime: 2020-12-22 12:27:51
 */
// @zhouhuabo

/**
 * 通过from组件中的数据，转换成后台pager对象需要的json对象
 * @param from
 * @returns {{op: string, rules: Array}}
 */
export function generateQueryParams(from) {
  var rules = [];
  var condition = {
    'op': 'and',
    'rules': rules
  };
  let ignore = '';
  if (typeof from.ignoreKey !== 'undefined') {
    ignore = from.ignoreKey;
  }
  Object.keys(from).forEach(function(key) {
    if (from[key] !== '' && from[key] !== null && key !== 'ignoreKey' && key !== ignore) {
      rules.push({
        'op': 'like',
        'field': key,
        'value': from[key],
        'type': 'string'
      });
    }
  });
  return condition;
}

// @liupei
/**
 * 通过传入的数据，转换成后台pager对象需要的json对象
 * @param array data
 * @returns {{op: string, rules: Array}}
 */
export function dealCondition(data) {
  var rules = [];
  var condition = {
    'op': 'and',
    'rules': rules
  };
  if(data.length){
    data.forEach(item => {
      if(item.value !== '' && !item.key){
        rules.push({
          'op': item.op ? item.op : 'like',
          'field': item.field,
          'value': item.value,
          'type': item.type ? item.type : 'string'
        });
      }
    })
  }
  return condition;
}
